package com.neborosoft.FlyingDog.android.gui.dialogs;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import com.neborosoft.FlyingDog.R;
import com.neborosoft.FlyingDog.android.MyActivity;
import com.neborosoft.FlyingDog.android.gui.adapters.ViewPagerAdapters.SongsPagerAdapter;
import com.neborosoft.FlyingDog.logic.database.content.identified.Audio;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 02.11.12
 * Time: 23:20
 * To change this template use File | Settings | File Templates.
 */
public abstract class MainWindow{
    private TextView songNameView, artistNameView, albumNameView;
    private int currentSongIndex;

    private boolean fromUser = true;

    private SongsPagerAdapter songsPagerAdapter;

    private ViewPager songSwitcher;

    private void updateAudioInfo(Audio audio){
        songNameView.setText(audio.getName());
        artistNameView.setText(audio.getArtistName());
        albumNameView.setText(audio.getAlbumName());
    }

    private class OnSongChanged implements ViewPager.OnPageChangeListener{
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int i) {
            if(!fromUser)
                return;

            if(currentSongIndex > i){
                onPrevSong();
            }
            else if(currentSongIndex < i) {
                onNextSong();
            }

            currentSongIndex = i;
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }

    public MainWindow(Activity activity){
        songNameView = (TextView)activity.findViewById(R.id.songNameMainText);
        albumNameView = (TextView)activity.findViewById(R.id.albumNameMainText);
        artistNameView = (TextView)activity.findViewById(R.id.artistNameMainText);
        songSwitcher = (ViewPager)activity.findViewById(R.id.mainWindowAlbumArt);

        songSwitcher.setOnPageChangeListener(new OnSongChanged());

        songsPagerAdapter = new SongsPagerAdapter(songNameView.getContext()){
            @Override
            public int getRootLayoutId() {
                return R.layout.big_album_art;
            }
        };
        songSwitcher.setAdapter(songsPagerAdapter);
    }

    public void notifyAdapterDataSetChanged(){
        songsPagerAdapter.notifyDataSetChanged();
    }

    public void setCurrentSongIndex(int index){
        fromUser = false;
        songSwitcher.setCurrentItem(index);
        currentSongIndex = index;
        fromUser = true;
    }

    public abstract void onNextSong();
    public abstract void onPrevSong();
}
